package cn.hchaojie.javase;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.junit.Test;

import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;

import cn.hchaojie.javase.week2.Score;

public class TestTemplate {
	@Test
	public void test1() {
		String s = "hello world";
		
		int[] count = new int[26]; 
		
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			
			if ('A' <= c && c <= 'Z') {
				count[c - 'A']++;
			} else if ('a' <= c && c <= 'z') {
				count[c - 'A' - 32]++;
			}
		}
		
		for (char c = 'A'; c <= 'Z'; c++) {
			if (count[c - 'A'] > 0) {
				System.out.println(c + " - " + count[c - 'A']);
			}
		}
	}
	
	@Test
	public void test2() {
		String s = "hello world";
		
		Map<Character, Integer> stat = new TreeMap<>();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			
			boolean isLowerCase = 'a' <= c && c <= 'z';
			boolean isUpperCase = 'A' <= c && c <= 'Z';
			
			boolean isLetter = isLowerCase || isUpperCase;
			if (!isLetter) {
				continue;
			}
			
			if (isLowerCase) {
				c = (char) (c - 32);
			}
			
			if (stat.containsKey(c)) {
				stat.put(c, stat.get(c) + 1);
			} else {
				stat.put(c, 1);
			}
		}
		
		for (Entry<Character, Integer> entry : stat.entrySet()) {
			System.out.println(entry.getKey() + " - " + entry.getValue());
		}
	}
	
	@Test
	public void test3() {
		List<Integer> numbers = new LinkedList<>();
		for (int i = 1; i <= 10; i++) {
			numbers.add(i);
		}
		
		while (numbers.size() > 0) {
			int random = (int) (Math.random() * numbers.size());
			
			Integer lucky = numbers.remove(random);
			System.out.println(lucky);
		}
	}
	
	@Test
	public void test4() throws IOException {
		Map<String, List<Score>> scores = new HashMap<>();

		List<String> lines = Files.readAllLines(Paths.get("data/scores.csv"), Charset.forName("gbk"));
		lines.remove(0);
		for (String line : lines) {
			String[] split = line.split(",");
			String course = split[1];
			Score s = new Score(Double.parseDouble(split[0]), split[2], course);

			List<Score> scoresByCourse = scores.get(course);
			if (scoresByCourse == null) {
				scoresByCourse = new ArrayList<>();
				scores.put(course, scoresByCourse);
			}
			
			scoresByCourse.add(s);
		}
		
		for (Entry<String, List<Score>> entry : scores.entrySet()) {
			System.out.println(entry.getKey());
			
			for (Score s : entry.getValue()) {
				System.out.println(s);
			}
		}
	}
}
